/*
* @Author: dengjiayao
* @Date:   2017-04-10 14:57:41
 * @Last Modified by: songwh
 * @Last Modified time: 2017-07-28 14:11:21
*/

import { modal } from 'vue-strap'
import { showMsg, saveClassifyItem, toogleSectionListFresh } from '../../../vuex/action'

const validation = {
  fromData(data) {
    for (let name in data) {
      if ((data[name] + '').length === 0) {
        return false
      }
    }
    return true
  },
  time(start, end) {
    return start < end
  }
}
export default {
  name: 'classify-new',
  components: {
    'vue-modal': modal
  },
  vuex: {
    actions: {
      xShowMsg: showMsg,
      xToogleIndexAdListFresh: toogleSectionListFresh,
      xSaveClassifyItem: saveClassifyItem
    }
  },
  props: ['showClassifyNew', 'classifyIt', 'title'],
  data() {
    return {
    }
  },
  computed: {
  },
  ready() {
  },
  methods: {
    submit() {
      var params = {
        sectionId: this.classifyIt.sectionId,
        name: this.classifyIt.name,
        showCount: this.classifyIt.showCount
      }
      if (this.classifyIt.classifyId){
        params.classifyId = this.classifyIt.classifyId
      }
      var valid = validation.fromData(params)
      if (!valid) {
        return this.xShowMsg({
          type: 'danger',
          content: '* 标记的均为必填/必选项'
        })
      }
      this.xSaveClassifyItem(params)
        .then((data) => {
          this.showClassifyNew= false
          this.xShowMsg({
            type: 'danger',
            content: '操作成功！'
          })
        })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    }
  },
  watch: {
    showClassifyNew() {
      if (!this.showClassifyNew) {
        this.xToogleIndexAdListFresh()
      }
    }
  }
}
